/*
 * Project: Sudoku Hint Engine for Android
 * Copyright (C) 2010-2013 Emanuel Moecklin
 * 
 * Available under the terms of the Lesser General Public License (LGPL)
 */
package com.onegravity.sudoku.solver.checks;

import java.util.ArrayList;
import java.util.Collection;

import android.net.Uri;

import com.onegravity.sudoku.solver.HintResource;
import com.onegravity.sudoku.solver.HintType;
import com.onegravity.sudoku.solver.core.FinalHint;
import com.onegravity.sudoku.solver.core.Rule;
import com.onegravity.sudoku.solver.core.WarningHint;
import com.onegravity.sudoku.solver.data.Cell;
import com.onegravity.sudoku.solver.data.Grid;
import com.onegravity.sudoku.solver.data.ImmutableCell;
import com.onegravity.sudoku.solver.util.HtmlLoader;

public class IncorrectDigitsWarning extends WarningHint implements Rule, FinalHint {

	private static final long serialVersionUID = -4302698125420567427L;
	
	private ArrayList<Cell> cells;
	private ArrayList<ImmutableCell> removedDigits;
	private String cellString;
	private boolean detailedMessage;
	
	/**
	 * @param cells
	 * @param detailedMessage if True the toString will return all the wrong cells,
	 * if False only a general message will be created
	 */
	public IncorrectDigitsWarning(Grid grid, ArrayList<Cell> cells, boolean detailedMessage) {
		super(grid);
		this.cells = cells;
		this.detailedMessage = detailedMessage;
		removedDigits = new ArrayList<ImmutableCell>();
		cellString = Cell.toString(cells.toArray(new Cell[cells.size()]));
	}

	@Override
    public Grid apply() {
    	for (Cell cell:cells) {
			ImmutableCell immutableCell = new ImmutableCell(cell);
			removedDigits.add(immutableCell);
			cell.setValue(0, cell.isGiven());
		}
    	return (cells!=null) ? cells.get(0).getGrid() : null;
	}

    @Override
	public String getName() {
		return cells.size()!=1 ? HintResource.INCORRECT_DIGITS.getName() : HintResource.INCORRECT_DIGIT.getName();
	}

    @Override
    public Uri getLink() {
		return cells.size()!=1 ? HintResource.INCORRECT_DIGITS.getUrl() : HintResource.INCORRECT_DIGIT.getUrl();
    }

	@Override
	public double getDifficulty() {
		return 0;
	}

	@Override
	public String toString() {
		return (cells.size()!=1) ?
				(detailedMessage ? HintResource.INCORRECT_DIGITS.toString(cellString) : HintResource.INCORRECT_DIGITS_NODETAILS.toString()) :
				HintResource.INCORRECT_DIGIT.toString(cellString);
	}

	@Override
	public String getHintHtml(HintType type) {
        String result = HtmlLoader.loadHtml((cells.size()!=1) ? HintResource.INCORRECT_DIGITS.hint(type) : HintResource.INCORRECT_DIGIT.hint(type));
        return HtmlLoader.format(result, cellString, cells.size());
	}

	@Override
    public Collection<ImmutableCell> getRemovedDigits(HintType hintType) {
		return (hintType==HintType.BIG_CLUE) ? removedDigits : null;
    }

    public int getNrOfSteps() {
    	return 2;
    }
}